package com.tiancheng.trade.merchant.util.Queue;

import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.merchant.dto.message.MerchantStatusMsg;
import com.tiancheng.trade.merchant.enums.MerchantQueueEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author jiangjun
 * @description 取消订单消息的发出者
 * @date 2024/12/29 23:39
 */
@Component
@Slf4j
public class MerchantRegisterSender {

    @Resource
    private AmqpTemplate amqpTemplate;

    /**
     * 发送延迟消息，毫秒
     * @param message
     * @param delayTimes
     */
    public void sendMessage(MerchantStatusMsg message, final long delayTimes){
        String messageStr = JsonUtil.toJsonString(message);
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(MerchantQueueEnum.QUEUE_TTL_MERCHANT_REGISTER_CANCEL.getExchange(), MerchantQueueEnum.QUEUE_TTL_MERCHANT_REGISTER_CANCEL.getRouteKey(), messageStr, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //给消息设置延迟毫秒值
                message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                return message;
            }
        });
        log.info("发送商户状态变更消息:{}",message.toString());
    }
}
